package com.example.capsule_chat0322.activity;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.example.capsule_chat0322.R;
import com.example.capsule_chat0322.adapter.ChatRoomListAdapter;
import com.example.capsule_chat0322.bean.MessageBean;
import com.example.capsule_chat0322.global.GlobalThreadPool;
import com.example.capsule_chat0322.netty.NettyClient;

import java.util.concurrent.ThreadPoolExecutor;

/**
 * @Author zhuzhiming
 * @Date 2022/5/1 18:30
 */
public class ChatRoomListActivity extends AppCompatActivity {


    ThreadPoolExecutor threadPoolExecutor;
    private NettyClient nettyClient;
    private volatile String isConnect;
    private ChatRoomListAdapter chatRoomListAdapter;
    private Button btn_connect01;
    private Button btn_connect02;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.chat_room_list);
        initView();//初始化UI控件
        initData();
        initListener();//初始化数据
    }

    private void initView() {
//        btn_connect01 = findViewById(R.id.btn_connect01);
//        btn_connect02 = findViewById(R.id.btn_connect02);
    }


    private void initData() {
        threadPoolExecutor = GlobalThreadPool.buildThreadPool();
        nettyClient = NettyClient.getInstance();
        threadPoolExecutor.execute(() -> {
            try {
                nettyClient.connectServer(getApplicationContext());
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });

        int count = 0;
        while (true) {
            isConnect = System.getProperty("isConnect");
            if (isConnect != null && isConnect.equals("true")) {
                break;
            }
            count++;
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            if (count > 6) {
                break;
            }
        }

    }


    public void initListener() {

        if (isConnect == null || "false".equals(isConnect)) {
            initData();
        }

        if (isConnect == null || "false".equals(isConnect)) {
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            // 设置属性
            builder.setTitle("错误");
            builder.setIcon(R.drawable.self);
            // 若是只是展现一个文本,只要设置了Message 就只会显示message
            builder.setMessage("连接不上服务器");
            // 积极的按钮 肯定
            // 能够连续.来调用方法
            builder.setPositiveButton("我知道啦", new DialogInterface.OnClickListener() {

                // 按钮上面的which永远返回-1
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    Toast.makeText(getApplicationContext(), "", Toast.LENGTH_SHORT)
                            .show();
                }
            })
                    //.setNegativeButton("狠心离开", null)
                    //中立的
                    .setNeutralButton("狠心离开", null);
            // 生成部分
            AlertDialog dialog = builder.create();
            dialog.show();
            return;
        }
        btn_connect01.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                MessageBean messageBean = new MessageBean();
                messageBean.setRoomName("1");
                messageBean.setSenderId("100");
                messageBean.setActionType(0);
                messageBean.setOneToOne(false);
                messageBean.setContent("");
                nettyClient.sendMessage(messageBean);
                Intent intent = new Intent(getApplicationContext(), ChatActivity.class);
                startActivity(intent);

            }
        });


    }


}
